﻿'XBMC4LCDSmartie
'#################################
'Plugin for LCDSmartie that displays information from XBMC
'
'(c) Oliver Tomkins

Imports Newtonsoft.Json.Linq
Imports Newtonsoft.Json
Imports System.IO
Imports System.Net
Imports System.Text
Imports System.Configuration

Public Class LCDSmartie

    'Parameters from config section
    Public sHost As String = System.Configuration.ConfigurationManager.AppSettings("SABnzbd4LCDSmartie.Host")
    Public iPort As Int32 = System.Configuration.ConfigurationManager.AppSettings("SABnzbd4LCDSmartie.Port")
    Public sUser As String = System.Configuration.ConfigurationManager.AppSettings("SABnzbd4LCDSmartie.User")
    Public sPass As String = System.Configuration.ConfigurationManager.AppSettings("SABnzbd4LCDSmartie.Pass")
    Public sApiKey As String = System.Configuration.ConfigurationManager.AppSettings("SABnzbd4LCDSmartie.ApiKey")
    Public iRefresh As Int32 = System.Configuration.ConfigurationManager.AppSettings("SABnzbd4LCDSmartie.Refresh")
    Public iRefreshInt As Int32 = System.Configuration.ConfigurationManager.AppSettings("SABnzbd4LCDSmartie.RefreshInt")

    Public jSABStatus As JObject
    Public jSABDownloadStatus As JObject
    Public tLastRefresh As DateTime

    'This funktion returns if SABnzbd is currently downloading or not
    '
    Public Function function1(ByVal param1 As String, ByVal param2 As String) As String

        Return SABGetStatus().ToString

    End Function

    'This function return the current download speed without spaces
    Public Function function2(ByVal param1 As String, ByVal param2 As String) As String

        Return SABGetItem("speed").ToString.Replace(" ", "")

    End Function

    'This function returns a valid item from the JSON-Stream back to LCDSmartie
    '#########################################################################
    'param1:
    'Valid Item
    '
    'param2:
    'Not used
    '
    Public Function function10(ByVal param1 As String, ByVal param2 As String) As String

        If param2 = "1" Then
            Return RemoveSpecialChars(SABGetItem(param1).ToString)
        Else
            Return SABGetItem(param1).ToString
        End If

    End Function

    'This function return a valid item from the JSON-Stream (slots subsection) back to LCDSmartie
    '#########################################################################
    'param1:
    'Valid Item
    '
    'param2:
    'Not used
    '
    Public Function function11(ByVal param1 As String, ByVal param2 As String) As String

        If param2 = "1" Then
            Return RemoveSpecialChars(SABGetDownloadItem(param1).ToString)
        Else
            Return SABGetDownloadItem(param1).ToString
        End If

    End Function


    Public Function GetMinRefreshInterval() As Integer

        Dim settings As New My.MySettings

        Return iRefreshInt   ' 300 ms (around 3 times a second)

    End Function

    Function JSONRead()

        Dim settings As New My.MySettings
        Dim jObject As JObject
        Dim sServerPath = "http://" & sHost & ":" & iPort & "/sabnzbd/api?mode=queue&start=START&limit=LIMIT&output=json&apikey=" & sApiKey

        Dim wc = New WebClient
        wc.Headers("Contect-Type") = "application/json"
        wc.Encoding = Encoding.UTF8

        If sUser <> "" Then
            wc.Credentials = New NetworkCredential(sUser, sPass)
        End If

        'Console.WriteLine(Date.Now.ToString("hh:mm:ss.fff"))

        'Console.WriteLine(wc.UploadString(sServerPath, sCommand))

        jObject = jObject.Parse(wc.UploadString(sServerPath, "Nothing"))

        'Console.WriteLine(Date.Now.ToString("hh:mm:ss.fff"))

        Return jObject

    End Function

    Function FillJSONRecords()

        If jSABStatus Is Nothing Then
            'MsgBox("New")
            jSABStatus = JSONRead()
            tLastRefresh = Now
            'MsgBox(DateTime.Compare(tLastRefresh.AddSeconds(My.Settings.SABRefresh), Now))
        Else
            If DateTime.Compare(tLastRefresh.AddSeconds(iRefresh), Now) < 0 Then
                jSABStatus = JSONRead()
                jSABDownloadStatus = Nothing
                tLastRefresh = Now
            End If
        End If

        For Each QueueItem In jSABStatus.SelectToken("queue.slots")
            If QueueItem.SelectToken("status") = "Downloading" Then
                jSABDownloadStatus = QueueItem
                Exit For
            End If
        Next

        Return Nothing

    End Function

    Function RemoveSpecialChars(ByVal sString As String)

        'Replace Special Chars that cannot be displayed correctly
        Dim sArray(5, 1) As String

        sArray(0, 0) = "ö" : sArray(0, 1) = "oe"
        sArray(1, 0) = "ü" : sArray(1, 1) = "ue"
        sArray(2, 0) = "ä" : sArray(2, 1) = "ae"
        sArray(3, 0) = "ß" : sArray(3, 1) = "ss"
        sArray(4, 0) = "[" : sArray(4, 1) = "("
        sArray(5, 0) = "]" : sArray(5, 1) = ")"

        For i = 0 To (sArray.Length / 2) - 1
            sString = sString.Replace(sArray(i, 0), sArray(i, 1))
        Next

        Return sString

    End Function

    Function SABGetStatus()

        FillJSONRecords()

        Return jSABStatus.SelectToken("queue.status")

    End Function

    Function SABGetItem(ByVal sItem As String)

        FillJSONRecords()

        Return jSABStatus.SelectToken("queue." & sItem)

    End Function

    Function SABGetDownloadItem(ByVal sItem As String)

        FillJSONRecords()

        If jSABDownloadStatus Is Nothing Then

            Return "No Download"

        Else

            Return jSABDownloadStatus.SelectToken(sItem)

        End If

    End Function


End Class

